﻿using System.Diagnostics;
using HalalCheck.CustomViews;
using HalalCheck.CustomViews.Shapes;
using HalalCheck.DataModels;
using HalalCheck.Resources;
using HalalCheck.ViewModels;
using SpotOffer.Resource;
using Xamarin.Forms;
using XLabs.Forms.Controls;
using XLabs.Forms.Mvvm;

namespace HalalCheck.View
{
    public class HalalListPage : BasePage
    {
        private ExtendedEntry tc;

        public HalalListPage()
        {
            Title = HalalStrings.ENumbers;

            var layout = new RelativeLayout
            {
                VerticalOptions = LayoutOptions.FillAndExpand,

            };
            //  layout.Children.Add(new TileViewHalal(), Constraint.Constant(0), Constraint.Constant(0), Constraint.RelativeToParent((p => p.Bounds.Width)), Constraint.RelativeToParent((p) => p.Bounds.Height));

            var halalchecklogo = new Image
            {
                Source = "centerlogo.png"
            };
            layout.Children.Add(halalchecklogo, Constraint.RelativeToParent((p) => (p.Bounds.Width - 200) / 2), Constraint.RelativeToParent((p) => (p.Bounds.Height - 200) / 2), Constraint.RelativeToParent((p => 200)), Constraint.RelativeToParent((p) => 200));

            tc = new ExtendedEntry
           {
               BackgroundColor = AppColors.White,
               TextColor = Color.Gray,
               Placeholder = HalalStrings.Search,
               
                PlaceholderTextColor = Color.Gray,
             //  Keyboard = Keyboard.Numeric
           };
            tc.SetBinding(Entry.TextProperty, new Binding("Search"));

            var listhalal = new ListView
            {
                VerticalOptions = LayoutOptions.FillAndExpand,
                SeparatorVisibility = SeparatorVisibility.None,
 
                BackgroundColor = Color.Transparent
            };
            listhalal.SetBinding(ListView.ItemsSourceProperty, new Binding("HalalList"));
            listhalal.ItemSelected += (sender, e) =>
            {
                if (e.SelectedItem == null)
                    return; // don't do anything if we just de-selected the row
                Navigation.PushAsync(ViewFactory.CreatePage<HalalItemViewModel, HalalItemPage>((v, p) => { v.SelectedHalalItem = (HalalItem)e.SelectedItem; }) as Page, true);
                ((ListView)sender).SelectedItem = null; // de-select the row
            };
            //  _dealListView.RowHeight = (int)(Rowheight);
            listhalal.ItemTemplate = new DataTemplate(MakeHalalItemLayout);

            listhalal.HasUnevenRows = false;
            listhalal.RowHeight = 100;
            layout.Children.Add(tc, Constraint.Constant(0), Constraint.Constant(0), Constraint.RelativeToParent((p) => p.Bounds.Width));
            layout.Children.Add(listhalal, Constraint.Constant(AppMargins.ItemPadding), Constraint.RelativeToView(tc, (p, s) => s.Bounds.Bottom + AppMargins.ItemPadding), Constraint.RelativeToParent((p) => p.Bounds.Width - AppMargins.ItemPadding * 2), Constraint.RelativeToView(tc, ((relativeLayout, view) => relativeLayout.Bounds.Height - view.Bounds.Height - AppMargins.ItemPadding * 2)));
            Content = layout;
        }



        private ViewCell MakeHalalItemLayout()
        {
            var itemLayout = new RelativeLayout
            {
                HorizontalOptions = LayoutOptions.FillAndExpand,
                VerticalOptions = LayoutOptions.FillAndExpand,

            };
            var background = AppViews.WhiteRounded();

            itemLayout.Children.Add(background, Constraint.Constant(0), Constraint.Constant(0), Constraint.RelativeToParent((p) => (p.Bounds.Width)), Constraint.RelativeToParent((p) => (p.Bounds.Height - AppMargins.ItemPadding)));

            var halalLabelBackground = AppViews.WhiteRounded();
            halalLabelBackground.SetBinding(BackgroundColorProperty, new Binding("HalalColor"));
            itemLayout.Children.Add(halalLabelBackground, Constraint.Constant(AppMargins.ItemMargin), Constraint.Constant(AppMargins.ItemMargin), Constraint.Constant(100), Constraint.RelativeToView(background, (parent, s) => s.Bounds.Height - AppMargins.ItemMargin * 2));

            var halalLabel = new Label
            {
                XAlign = TextAlignment.Center,
                YAlign = TextAlignment.Center,
                FontAttributes = FontAttributes.Bold,
                FontSize = (Device.GetNamedSize(NamedSize.Medium, typeof(Label))),
                TextColor = AppColors.White,
                //FontName = "Harabara.otf",
            };
            halalLabel.SetBinding(Label.TextProperty, "HalalText");

            itemLayout.Children.Add(halalLabel, Constraint.Constant(AppMargins.ItemMargin), Constraint.Constant(AppMargins.ItemMargin), Constraint.Constant(100), Constraint.RelativeToView(background, (parent, s) => s.Bounds.Height - AppMargins.ItemMargin * 2));

            var halalid = new Label
            {
                XAlign = TextAlignment.Start,
                YAlign = TextAlignment.Start,
                FontSize = (Device.GetNamedSize(NamedSize.Large, typeof(Label))),
                FontAttributes = FontAttributes.Bold,
                TextColor = AppColors.OrigDarkBlue,
                LineBreakMode = LineBreakMode.TailTruncation,
                //FontName = "Harabara.otf",
            };
            halalid.SetBinding(Label.TextProperty, new Binding("Id"));
            itemLayout.Children.Add(halalid, Constraint.RelativeToView(halalLabel, (p, s) => (s.Bounds.Right + AppMargins.ItemPadding)), Constraint.Constant(AppMargins.ItemPadding), Constraint.RelativeToView(halalLabel, (p, s) => (p.Bounds.Width - s.Bounds.Width - AppMargins.ItemPadding * 3)));

            var halaltext = new Label
            {
                XAlign = TextAlignment.Start,
                YAlign = TextAlignment.Start,
                FontSize = (Device.GetNamedSize(NamedSize.Medium, typeof(Label))),
                TextColor = AppColors.OrigDarkBlue,
                LineBreakMode = LineBreakMode.TailTruncation,
                //FontName = "Harabara.otf",
            };
            halaltext.SetBinding(Label.TextProperty,"Name");
            itemLayout.Children.Add(halaltext, Constraint.RelativeToView(halalLabel, (p, s) => (s.Bounds.Right + AppMargins.ItemPadding)), Constraint.RelativeToView(halalid, (p, s) => (s.Bounds.Bottom)), Constraint.RelativeToView(halalLabel, (p, s) => (p.Bounds.Width - s.Bounds.Width - AppMargins.ItemPadding * 3)));

            return new ViewCell
            {
                View = itemLayout
            };
        }

        protected override void OnAppearing()
        {
            base.OnAppearing();
          //  tc.Focus();
        }
    }
}
